import Vue from 'vue'
import Vuex from 'vuex'



Vue.use(Vuex);

/**
 * Vuex全局状态管理
 * @param options {Array} 用于渲染tabs的数组
 */
const store = new Vuex.Store({
	state: {
		userInfo: {},
		selectedRouters: [{
			title: '首页',
			index: '1',
			route: '/'
		}],
		menuList: [
                    {
                      title:'导航一',
                      index: '1',
                      subs:[
                        {title:'选项1', route:'/page1', index:'menu1'},
                        {title:'选项2', route:'/page2', index:'menu2'},
                        {title:'选项3', route:'/page3', index:'menu3'}
                      ]
                    },
                    {title:'导航二', route:'/nav1/33390', index:'menu4'},
                    {title:'导航三', route:'/nav2/2', index:'menu5'}
        ],
		menuListMap:{
            '/page1':{title:'选项1', route:'/page1', index:'menu1'},
            '/page2':{title:'选项2', route:'/page2', index:'menu2'},
            '/page3':{title:'选项3', route:'/page3', index:'menu3'},
            '/nav1/33390':{title:'导航二', route:'/nav1/33390', index:'menu4'},
            '/nav2/2':{title:'导航三', route:'/nav2/2', index:'menu5'}
        },
		currentActiveIndex:'1',
		currentMenuItem:'/'
	},
	getters: {
		doneTodos: state => {
			return state.activeIndex
		}
	},
	mutations: {
		addTab(state, data) {
			state.selectedRouters.push(data)
		},
		// 删除tabs
		deleteTab(state, index) {
			for(let i=0, len=state.selectedRouters.length; i<len; i++){
				if(state.selectedRouters[i].index === index){
					state.selectedRouters.splice(i, 1)
					return
				}
			}
		},
		clearTabs(state) {
			state.selectedRouters.splice(0)
		},
		testMap(state, sign) {
			alert('mapping successfully: ' + sign)
		},
		// 设置当前激活的tab
		setCurrentActiveIndex(state, index){
			state.currentActiveIndex = index
		},
		setCurrentMenuItem(state, route){
			state.currentMenuItem = route
		},
		// 设置用户详情信息
		saveUserInfo(state, info) {
			this.state.userInfo = info;
		}
	}
});

export default store;